<?php
class BaseField{
	var $typeName = "";
	var $name = "";
	var $formName = "";
	var $template = "";
	var $properties = array();
	var $required = FALSE;
	var $meta = FALSE;
	var $meta_name = NULL;
	
	var $ci;
	
	/*
	 * Contenido estatico asociado
	 */
	var $static = array(
		"javascript" => array(
		),
		"style" => array(
		)		
	);
	
	function BaseField(){
		$this->typeName = strtolower(get_class($this));
		$this->ci =& get_instance();
		
		$this->ci->load->model("Node_Model", "Nodes");
	}
	
	function setTypeName($name){
		$this->typeName = $name;
	}
	
	function getTypeName(){
		return $this->typeName;
	}
	
	function setFormName($name){
		$this->formName = $name;
	}
	
	function getFormName(){
		return $this->formName;
	}
	
	function setName($name){
		$this->name = $name;
	}
	
	function getName(){
		return $this->name;
	}
	
	function getRequired(){
		return $this->required;
	}
	
	function setRequired($required){
		$this->required = $required;
	}
	
	function getMeta(){
		return $this->meta;
	}
	
	function setMeta($meta){
		$this->meta = $meta;
	}
	
	function getMetaName(){
		return $this->meta_name;
	}
	
	function setMetaName($meta_name){
		$this->meta_name = $meta_name;
	}
	
	function addProperty($name, $value){
		$this->properties[$name] = $value;
	}
	
	function getProperty($name){
		if(isset($this->properties[$name])){
			return $this->properties[$name];
		}
		
		return FALSE;
	}
	
	function addProperties($properties){
		if($properties != NULL){
			foreach($properties as $key=>$value){
				$this->addProperty($key, $value);
			}
		}
	}
	
	function getProperties(){
		return $this->properties;
	}
	
	function addStatic($type, $path){
		
		if(isset($this->static[$type])){
			if(array_search($path, $this->static[$type]) === FALSE){
				array_push($this->static[$type], $path);
				//print_r($this->static);
			} 
		}
	}
	
	function getStatics(){
		return $this->static;
	}
	
	
	function view($node = NULL, $data = NULL){
		$value = $data;
		$name = $this->formName;
		$properties = $this->properties;
		$required = $this->required;
		
		ob_start();
		eval('?>'.preg_replace("/;*\s*\?>/", "; ?>", str_replace('<?=', '<?php echo ', file_get_contents($this->template, TRUE))));
		$view = ob_get_contents();
		@ob_end_clean();
		
		return $view;
	}
	
	function checkCorrectSave(){
		if($this->getRequired()){
			$this->ci->form_validation->set_rules($this->formName, $this->properties['title'], 'required');
		} else{
			//Trick para poder hacer el set_value
			$this->ci->form_validation->set_rules($this->formName, "", "");
		}
		
		if(!$this->ci->form_validation->run()){
			return FALSE;
		}
		
		return TRUE;
	}
	
	function save($node = NULL, $data = NULL){
		/*if($this->getRequired()){
			$this->ci->form_validation->set_rules($this->formName, $this->properties['title'], 'required');
		}
		*/
		if($this->ci->form_validation->run()){
			$value = $this->ci->input->post($this->formName);
			if($value != NULL){
				//$data[$this->name] = $value;
				
				if($node != NULL){
					if($this->meta){
						$this->processMetaValue($node, $value);
					}
				}
				
				return $value;
			}
		} else{
			//echo "CAMPO " . $this->name . " REQUERIDO<br/>";
			//@TODO volver al formulario hasta que este correcto...
		}
		
		return NULL;
	}
	
	function processMetaValue($node = NULL, $value = NULL){
		if($this->meta && $value != NULL){
			$value = strip_tags($value);
			
			//Añadimos el campo meta
			$meta_name = $this->name;
			if($this->meta_name != NULL){
				$meta_name = $this->meta_name;
			}
			
			$meta_value = $this->ci->Nodes->getMeta($node->id, $meta_name, TRUE);
			if($meta_value == NULL){
				$this->ci->Nodes->addMeta($node->id, $meta_name, $value);
			} else{
				$this->ci->Nodes->updateMeta($node->id, $meta_name, $value);
			}
		}
	}
	
	function delete($node = NULL, $data = NULL){
		return TRUE;
	}
}
?>